package com.itheima.test;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();

        Scanner sc = new Scanner(System.in);

       abc: while(true) {
            // 1. 搭建主界面菜单
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            //接收键入变量,使用Scanner实现
            int choice = sc.nextInt();
            //switch循环实现功能选择
            switch(choice){
                case 1:
                    //System.out.println("添加学生功能");
                    addStudent(list);
                    break;
                case 2:
                    //System.out.println("删除学生功能");
                    removeStudent(list);
                    break;
                case 3:
                    //System.out.println("修改学生功能");
                    updateStudent(list);
                    break;
                case 4:
                    //System.out.println("查看学生功能");
                    showAllStudent(list);
                    break;
                case 5:
                    System.out.println("感谢您的使用");
                    //break abc;
                    return;



            }


        }
    }

    public static void updateStudent(ArrayList<Student> list) {
        //键盘录入要修改的学生学号
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生的学号");
        String updateSid = sc.next();
        //判断输入学号是否在学生对象中
        int index = getIndex(list,updateSid);
        if (index == -1){
            System.out.println("查无信息,请重新输入");
        }else{
            System.out.println("请输入新姓名");
            String name = sc.next();
            System.out.println("请输入新年龄");
            String age = sc.next();
            System.out.println("请输入新生日");
            String birthday = sc.next();
            /*//创建新学生对象
            Student newStu = new Student(updateSid,name,age,birthday);
            //替换要修改的学生对象
            list.set(index,newStu);
            System.out.println("修改成功");*/
            Student stu = list.get(index);
            stu.setName(name);
            stu.setAge(age);
            stu.setBirthday(birthday);
            System.out.println("修改成功");
        }
    }

    public static void removeStudent(ArrayList<Student> list) {
        //键盘录入学号信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入所要删除学生的学号信息");
        String sid = sc.next();
        //接收学号所对应对象的元素索引值
        int index = getIndex(list,sid);

                if(index == -1){
                    System.out.println("未查询到该学号");

                }else{
                    list.remove(index);
                    System.out.println("删除成功");
                }
     /*   for (int i = 0; i < list.size(); i++) {
            if (sid.equals(list.get(i).getSid())){
                list.remove(i);
                i--;
                System.out.println("删除成功");
            }else{
                System.out.println("未查询到该学生");
            }
        }*/


    }
    public static int getIndex(ArrayList<Student> list,String sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if(sid.equals(list.get(i).getSid())){
                index = i;
            }

        }return index;

    }

    public static void showAllStudent(ArrayList<Student> list) {
        //判断是否有学生信息
        if(list.size() == 0){
            System.out.println("无信息,请添加后重新查询");
        }else{
            //展示学生信息
            System.out.println("学号\t\t姓名\t年龄\t生日");
            //遍历集合,输出所有学生信息
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getSid()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t\t" + stu.getBirthday());            }
        }



    }

    public static void addStudent(ArrayList<Student> list) {
        //录入学生信息
        Scanner sc = new Scanner(System.in);
        //用户提示
        System.out.println("请输入学号:");
        String sid = sc.next();
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        String age = sc.next();
        System.out.println("请输入生日:");
        String birthday = sc.next();
        //将接收到的学生信息存入集合容器中
        Student stu = new Student(sid,name,age,birthday);
        list.add(stu);
        System.out.println("添加成功");
    }


}
